package ${basePackage}.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.itigger.commons.service.BaseService;
import ${basePackage}.domain.${className}VO;
import ${basePackage}.mapper.${className}Mapper;
import cn.itigger.commons.util.Criteria;
import cn.itigger.commons.util.Page;
import cn.itigger.commons.util.Result;

@Service("${lowerName}Service")
public class ${className}Service extends BaseService {

	@Resource
	private ${className}Mapper ${lowerName}Mapper;

	/**
	 * 编辑${tableComment}
	 * @param ${lowerName}VO(${tableComment}实体)
	 * @return
	 */
	public Result edit(${className}VO ${lowerName}VO){
		Result result = new Result();
		try {
			if(${lowerName}VO.getId()!=null&&${lowerName}VO.getId()>0){
				${lowerName}Mapper.updateBySelective(${lowerName}VO);
			}else{
				${lowerName}Mapper.add(${lowerName}VO);
			}
			
			result.setModel("${lowerName}",${lowerName}VO);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		return result;
	}
	
	/**
	 * 删除${tableComment}
	 * @param id(编号)
	 * @return
	 */
	public Result delete(Integer id){
		Result result = new Result();
		try {
			${lowerName}Mapper.deleteById(id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		return result;
	}
	
	/**
	 * 查询${tableComment}详情
	 * @param id(编号)
	 * @return
	 */
	public Result get(Integer id){
		Result result = new Result();
		try {
			${className}VO ${lowerName}VO = ${lowerName}Mapper.getById(id);
			result.setModel("${lowerName}",${lowerName}VO);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		return result;
	}

	/**
	 * 查询${tableComment}列表
	 * @param page(分页信息)
	 * @return
	 */
	public Result list(Criteria criteria){
		Result result = new Result();
		try {
		
			Page page = criteria.getPage();
			
			if(page!=null&&page.getPageSize()!=null){
				page.setTotalItem(${lowerName}Mapper.count(criteria));
			}
			
			List<${className}VO> list = ${lowerName}Mapper.list(criteria);
			
			if(page!=null&&page.getPageSize()!=null){
				result.setModel("page",page);
			}
			
			result.setModel("list",list);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		return result;
	}
}
